<?php

class Welcome extends CI_Controller {


	public function index()
	{
		// $this->load->view('welcome_message');
		$this->load->helper('url');
		echo base_url();
		$this->load->view('form.html');
	}

	public function handle()
	{
		// echo '我将对接收到的数据进行处理！';

		// 载入验证类
    	$this->load->library( 'form_validation' );

    	 
	    $this->form_validation->set_rules( 'stuname', '姓名', 'required' );


	    
	    if ( $this->form_validation->run() ) {
	      //do something
	    } 
	}
    public function test($value = '') { 
      $ch = curl_init("http://top.baidu.com"); 
  
       curl_setopt($ch, CURLOPT_HEADER, 0); 
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  
        $data = curl_exec($ch); 
      $str  = iconv("GB2312", "UTF-8", $data); 
 
       $pattern = '@<div id="box-today" class="box-today">.*?<a target="_blank" href=".*?">(?<title>.*?)</a>.*?事件.*?<a target="_blank" href=".*?">(.*?)</a>@s'; 
      preg_match($pattern, $str, $matches); 
       curl_close($ch); 
   } 
   
   
    public function get_notice_list() { 
     // 初始化curl类 
       $ch  = curl_init(); 
        $url = 'http://www.xbmu.edu.cn/frontChannel.action?siteId=1&articleClassId=104'; 
       // 设定curl搜索地址 
      curl_setopt($ch, CURLOPT_URL, $url); 
       // 设定curl不返回头信息 
      curl_setopt($ch, CURLOPT_HEADER, 0); 
        // 设定curl不在网页中直接显示抓取内容 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
       $content = curl_exec($ch); 
      curl_close($ch); 
       // 设置抓取正则表达式 
     $pattern = '/<li><span>(?<post_time>.*?)<\/span>.*?articleId=(?<post_id>\d*).*?\>(?<post_title>.*?)<\/a>.*?<\/li>/s'; 
        // // 执行正则表达式 
      preg_match_all($pattern, $content, $matched); 
       // 取前十条数据 
       for ($i = 0; $i < 10; $i++) { 
          $data[$i] = array( 
                'post_href'  => $matched['post_id'][$i], 
              'post_title' => $matched['post_title'][$i], 
           ); 
        } 
       var_dump($data); 
   } 
  
    public function info($value = '') { 
        phpinfo(); 
   } 
   
    public function curl($url = '') { 
  
       // 初始化curl函数 
      $ch = curl_init($url); 
 
       // 设置必要的配置信息 
       curl_setopt($ch, CURLOPT_HEADER, 0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
       // 执行curl 
       $data = curl_exec($ch); 
  
        // 关闭curl,释放资源 
        curl_close($ch); 
  
      return $data; 
    } 
 
  public function getStdNotice() { 
        $data = $this->curl('http://cms.xbmu.edu.cn/frontChannelPage.action?siteId=69&articleClassId=1904'); 
       $pattern = '@<a href="(?<url>.*?)" target="_blank">(?<title>.*?)</a>.*?发布日期:(?<date>.*?)</td>@s'; 
      preg_match_all($pattern, $data, $matches); 
        // echo '<pre>'; 
       // var_dump($matches['url'], $matches['title'], $matches['date']); 
       echo '<table>'; 
        for ($i = 0; $i < 5; $i++) { 
           echo '<tr><td><a href="' . $matches['url'][$i + 1] . '">' . $matches['title'][$i] . '</a></td><td>' . date('Y-m-d', strtotime($matches['date'][$i])) . '</td></tr>'; 
        } 
     echo '</table>'; 
    } 


	public function re($value='')
	{
		$pattern = '/^[\x80-\xff]{3,}$/';
		// $pattern = '/[1-4a-h]/';
		$string =   '中中中中中中中中中中';
		$this->outputRe($pattern, $string);
	}

	public function outputRe($pattern, $string)
	{

    if (preg_match_all($pattern, $string, $matched)) {
    echo '正则表达式:', htmlentities($pattern), '和字符串：', htmlentities($string), '匹配成功，匹配的字符串是：', "<br />";
    echo '<pre>';
    var_dump($matched);
    echo '</pre>';
  } else {
    echo "<p>",'没找到匹配字符', "</p>";
  }
}
}
